/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.surveilans.controller;

import com.surveilans.model.DataModel;
import com.surveilans.classes.DataSurveilans;
import com.surveilans.classes.Penyakit;
import com.surveilans.classes.Unit;
import com.surveilans.model.UnitModel;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Yudi Retanto
 */
@WebServlet(name = "UnitController", urlPatterns = {"/unit"})
public class UnitController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        UnitModel unit = new UnitModel();
        DataModel data = new DataModel();
        request.setAttribute("title", "Unit Surveilans");
        if (request.getParameter("action")==null) {
            if(request.getSession().getAttribute("authenticated")!=null){
                //logged in
                int unit_id = (Integer)request.getSession().getAttribute("unit_id");
                Unit u = unit.getUnit(unit_id);
                ArrayList<Penyakit> ap = data.getPenyakit();
                Date curdate = new Date();
                SimpleDateFormat sdfd = new SimpleDateFormat("d");
                SimpleDateFormat sdfm = new SimpleDateFormat("MMMM");
                SimpleDateFormat sdfy = new SimpleDateFormat("yyyy");
                
                request.setAttribute("unit", u);
                request.setAttribute("penyakit", ap);
                request.setAttribute("day", sdfd.format(curdate));
                request.setAttribute("month", sdfm.format(curdate));
                request.setAttribute("year", sdfy.format(curdate));
                request.getRequestDispatcher("data/add.jsp").forward(request, response);
            }else{
                response.sendRedirect("./home");
            }
        }else if (request.getParameter("action").equals("laporan")) {
            int unit_id = (Integer)request.getSession().getAttribute("unit_id");
            ArrayList<DataSurveilans> ad = data.getDataPenyakit(unit_id);
            request.setAttribute("data", ad);
            request.getRequestDispatcher("data/list.jsp").forward(request, response);
        }else if (request.getParameter("action").equals("delete")) {
            int id_data = Integer.valueOf(request.getParameter("id"));
            data.deleteDataSurveilans(id_data);
            request.getSession().setAttribute("flash_type", "success");
            request.getSession().setAttribute("flash", "Data surveilans telah berhasil dihapus dari sistem");
            response.sendRedirect("./unit?action=laporan");
        }else if (request.getParameter("action").equals("edit")) {
            if (request.getMethod().equals("POST")) {
                DataSurveilans ds = new DataSurveilans();
                ds.setId(Integer.valueOf(request.getParameter("id")));
                ds.setId_unit(Integer.valueOf(request.getParameter("id_unit")));
                ds.setRange1(Integer.valueOf(request.getParameter("pen[1]")));
                ds.setRange2(Integer.valueOf(request.getParameter("pen[2]")));
                ds.setRange3(Integer.valueOf(request.getParameter("pen[3]")));
                ds.setRange4(Integer.valueOf(request.getParameter("pen[4]")));
                ds.setRange5(Integer.valueOf(request.getParameter("pen[5]")));
                ds.setRange6(Integer.valueOf(request.getParameter("pen[6]")));
                ds.setRange7(Integer.valueOf(request.getParameter("pen[7]")));
                ds.setRange8(Integer.valueOf(request.getParameter("pen[8]")));
                ds.setRange9(Integer.valueOf(request.getParameter("pen[9]")));
                ds.setRange10(Integer.valueOf(request.getParameter("pen[10]")));
                ds.setRange11(Integer.valueOf(request.getParameter("pen[11]")));
                ds.setRange12(Integer.valueOf(request.getParameter("pen[12]")));
                data.updateDataSurveilans(ds);
                request.getSession().setAttribute("flash_type", "success");
            request.getSession().setAttribute("flash", "Data surveilans telah berhasil diedit");
                response.sendRedirect("./unit?action=laporan");
            }else{
                int id_data = Integer.valueOf(request.getParameter("id"));
                DataSurveilans ds = data.getDataSurveilans(id_data);
                request.setAttribute("data", ds);
                request.getRequestDispatcher("data/edit.jsp").forward(request, response);
            }
        }else if (request.getParameter("action").equals("add")) {
            if (request.getMethod().equals("POST")) {
                int unit_id = (Integer)request.getSession().getAttribute("unit_id");
                ArrayList<Penyakit> ap = data.getPenyakit();
                Iterator itr = ap.iterator();
                while (itr.hasNext()) {
                    DataSurveilans dp = new DataSurveilans();
                    Penyakit p = (Penyakit) itr.next();
                    dp.setId_penyakit(p.getId());
                    dp.setNama_penyakit(dp.getNama_penyakit());
                    dp.setId_unit(unit_id);
                    String idx = "pen"+p.getId();
                    dp.setRange1(Integer.valueOf(request.getParameter(idx+"[1]")));
                    dp.setRange2(Integer.valueOf(request.getParameter(idx+"[2]")));
                    dp.setRange3(Integer.valueOf(request.getParameter(idx+"[3]")));
                    dp.setRange4(Integer.valueOf(request.getParameter(idx+"[4]")));
                    dp.setRange5(Integer.valueOf(request.getParameter(idx+"[5]")));
                    dp.setRange6(Integer.valueOf(request.getParameter(idx+"[6]")));
                    dp.setRange7(Integer.valueOf(request.getParameter(idx+"[7]")));
                    dp.setRange8(Integer.valueOf(request.getParameter(idx+"[8]")));
                    dp.setRange9(Integer.valueOf(request.getParameter(idx+"[9]")));
                    dp.setRange10(Integer.valueOf(request.getParameter(idx+"[10]")));
                    dp.setRange11(Integer.valueOf(request.getParameter(idx+"[11]")));
                    dp.setRange12(Integer.valueOf(request.getParameter(idx+"[12]")));
                    data.addData(dp);
                }
                request.getSession().setAttribute("flash_type", "success");
                request.getSession().setAttribute("flash", "Data surveilans telah berhasil ditambahkan ke sistem");
                request.getSession().setAttribute("unit_add", true);
                response.sendRedirect("./unit");
            }else{
                response.sendRedirect("./unit");
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
